/*
 * @Author: chendeji
 * @Date: 2021-11-16 10:14:41
 * @LastEditTime: 2021-11-28 09:58:15
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\common\behavior\base_loading_behavior.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'package:copy_gsy_github_app_flutter/common/style/gsy_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';

import 'i_loading_behavior.dart';

/// 使用单例，每个加载界面都需要使用
/// 使用Get显示弹窗
class ShowLoadingBehavior implements ILoadingBehavior {
  static ShowLoadingBehavior? _instance;

  static ShowLoadingBehavior get instance => _getInstance();
  ShowLoadingBehavior._internal();

  factory ShowLoadingBehavior() => _getInstance();

  static ShowLoadingBehavior _getInstance() {
    _instance = _instance ?? ShowLoadingBehavior._internal();
    return _instance!;
  }

  @override
  void finishLoad() {
    bool open = Get.isDialogOpen ?? false;
    if (open) {
      Get.back();
    }
  }

  @override
  void loading() {
    bool open = Get.isDialogOpen ?? false;
    if (!open) {
      Get.dialog(_LoadingDialog(
        child: Container(
          child: Material(
              color: Colors.transparent,
              child: WillPopScope(
                onWillPop: () => new Future.value(false),
                child: Center(
                  child: new Container(
                    width: 200.0,
                    height: 200.0,
                    padding: new EdgeInsets.all(4.0),
                    decoration: new BoxDecoration(
                      color: Colors.transparent,
                      //用一个BoxDecoration装饰器提供背景图片
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new Container(
                            child: SpinKitCubeGrid(color: APPColors.white)),
                        new Container(height: 10.0),
                        new Container(
                            child: new Text('loading_text'.tr,
                                style: APPFont.normalTextWhite)),
                      ],
                    ),
                  ),
                ),
              )),
        ),
      ));
    }
  }
}

class _LoadingDialog extends StatefulWidget {
  _LoadingDialog({Key? key, required this.child}) : super(key: key);

  Widget child;

  @override
  __LoadingDialogState createState() => __LoadingDialogState();
}

class __LoadingDialogState extends State<_LoadingDialog> {
  @override
  Widget build(BuildContext context) {
    return MediaQuery(
        data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window)
            .copyWith(textScaleFactor: 1),
        child: _NeverOverScrollIndicator(
          needOverload: false,
          child: new SafeArea(child: widget.child),
        ));
  }
}

///去除ScrollView的Material效果
class _NeverOverScrollIndicator extends StatelessWidget {
  final bool needOverload;

  final Widget? child;

  _NeverOverScrollIndicator({this.child, this.needOverload = true});

  @override
  Widget build(BuildContext context) {
    return ScrollConfiguration(
      child: child!,
      behavior: NeverOverScrollBehavior(needOverload: needOverload),
    );
  }
}

class NeverOverScrollBehavior extends ScrollBehavior {
  final bool needOverload;

  NeverOverScrollBehavior({this.needOverload = true});

  @override
  Widget buildViewportChrome(
      BuildContext context, Widget child, AxisDirection axisDirection) {
    return child;
  }

  @override
  ScrollPhysics getScrollPhysics(BuildContext context) {
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
        if (needOverload) {
          return const BouncingScrollPhysics();
        }
        return const ClampingScrollPhysics();
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
      default:
        return const ClampingScrollPhysics();
    }
  }
}
